home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFskyedit - Star Fighter 3000 sky colours editor
- * Quit confirm dbox
- * Copyright (C) 2001 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdio.h>
-
- /* RISC OS library files */
- #include "event.h"
- #include "toolbox.h"
- #include "quit.h"
- #include "wimplib.h"
-
- /* My library files */
- #include "err.h"
- #include "msgtrans.h"
- #include "Macros.h"
- #include "TboxBugs.h"
- #ifdef FOCUS_CRAP
- #include "InputFocus.h"
- #endif
- #include "viewsmenu.h"
-
- /* Local headers */
- #include "PreQuit.h"
- #include "editsky.h"
-
- static ObjectId dbox_id = NULL_ObjectId;
- static int quit_sender;
-
- /* ----------------------------------------------------------------------- */
- /* Function prototypes */
-
- static ToolboxEventHandler _PreQuit_quit_handler;
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- void PreQuit_initialise(ObjectId PreQuit_id)
- {
- /* Record ID */
- dbox_id = PreQuit_id;
-
- /* Install handlers */
- EF(event_register_toolbox_handler(PreQuit_id, Quit_Quit, _PreQuit_quit_handler, NULL));
- #ifdef FOCUS_CRAP
- EF(event_register_toolbox_handler(PreQuit_id, Quit_AboutToBeShown, InputFocus_recordcaretpos, NULL));
- #endif
- }
-
- /* ----------------------------------------------------------------------- */
-
- bool TRY_QUIT(int task_handle)
- {
- ViewData *ihatethiscode;
- ObjectId editingwindow;
- int unsaved_count = 0;
- char number[16];
-
- editingwindow = ViewMenu_getfirst();
- while(editingwindow != NULL_ObjectId) {
- if(!E(toolbox_get_client_handle(0, editingwindow, (void **)&ihatethiscode))) {
- if(ihatethiscode->changed_since_save)
- unsaved_count++;
- }
- editingwindow = ViewMenu_getnext(editingwindow);
- }
-
- if(unsaved_count > 1) {
- sprintf(number, "%d", unsaved_count);
- RE(quit_set_message(0, dbox_id, msgs_lookup_sub1("PlurUNS", number)))
- }
- else {
- if(unsaved_count == 1)
- RE(quit_set_message(0, dbox_id, msgs_lookup("SingUNS")))
- else
- return true; /* safe to quit */
- }
-
- quit_sender = task_handle;
- RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, dbox_id, Toolbox_ShowObject_Centre, NULL, NULL_ObjectId, NULL_ComponentId))
- return false; /* don't want to quit */
- }
-
- /* ----------------------------------------------------------------------- */
- /* Private functions */
-
- static int _PreQuit_quit_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* We won't be alive to hear the MenusDeleted msg, so fake it */
- #ifdef FOCUS_CRAP
- RE(InputFocus_restorecaret())
- #endif
- if(quit_sender != 0) {
- /* Restart desktop shutdown */
- WimpKeyPressedEvent key_event;
- RE(wimp_get_caret_position(&key_event.caret))
- key_event.key_code = 0x1FC;
- RE(wimp_send_message(Wimp_EKeyPressed, &key_event, quit_sender, 0, NULL))
-
- #ifdef QUIT_ON_SHUTDOWN
- /* Quit immediately */
- exit(EXIT_SUCCESS);
- #else
- /* Do as Paint, Edit and Draw do - that is, discard all data */
- ObjectId editingwindow = ViewMenu_getfirst();
- while(editingwindow != NULL_ObjectId) {
- RE(toolbox_delete_object(0, editingwindow))
- editingwindow = ViewMenu_getnext(editingwindow);
- }
- #endif
-
- } else {
- /* Quit application */
- exit(EXIT_SUCCESS);
- }
- return 1; /* claim event */
- }
-